//https://pub.flutter-io.cn/packages?q=http

import 'package:dio/dio.dart'; //https://pub.flutter-io.cn/packages?q=dio

import 'package:http/http.dart'
as http; //https://blog.csdn.net/weixin_44051815/article/details/121612206

Future getIp() {
  final url = 'https://httpbin.org/ip';
  return http.get(Uri.parse(url)).then((res) {
    return res.body;
  });
}

postRequest() {
  var url = 'http://httpbin.org/post';
  http.post(Uri.parse(url), body: "name=dartlang&color=red").then((response) {
    print('响应状态：${response.statusCode}');
    print('响应数据：${response.body}');
  }).whenComplete(() {
    print('postRequest whenComplete 结束');
  });
}

//https://blog.csdn.net/Shinnexi/article/details/100831260

testGet() {
  var url = 'https://www.ddd.com/';
  http.get(Uri.parse(url))
    ..timeout(Duration(seconds: 10))
    ..then((handleSuccess))
    ..catchError(handleFailure);
}

handleSuccess(http.Response response) {
  print('访问成功 statusCode = ${response.statusCode}');
  print('响应结果：${response.body}');
}

handleFailure(error) {
  print('发生错误');
  print('错误信息：${error.meesage}');
}

/*
  问题

  关于Flutter报Cannot run with sound null safety
  https://blog.csdn.net/m0_38013946/article/details/121561591

  https://xiangzhihong.blog.csdn.net/article/details/119671596?spm=1001.2101.3001.6661.1&utm_medium=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRLIST%7EPayColumn-1-119671596-blog-121561591.235%5Ev35%5Epc_relevant_default_base&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRLIST%7EPayColumn-1-119671596-blog-121561591.235%5Ev35%5Epc_relevant_default_base&utm_relevant_index=1


  https://blog.csdn.net/csm19950220/article/details/126289066

 */

//dio get请求
dio_get() async {
  print('调用了 dio_get');
  try {
    Response response;
    //等待返回response
    response = await Dio().get("https://httpbin.org/ip");
    if (response.statusCode == 200) {
      print("网络请求结果 response = $response");
    } else {
      print('网络请求失败');
    }
  } catch (e) {
    print('网络请求异常 $e');
  }
}